package com.yhsoft.buyer.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yhsoft.bill.service.ISysYfzkService;
import com.yhsoft.bill.service.ISysYszkService;
import com.yhsoft.buyer.controller.response.BuyerAddressOutVo;
import com.yhsoft.buyer.controller.response.OrderDetailOutVo;
import com.yhsoft.buyer.controller.response.OrderInfoOutVo;
import com.yhsoft.buyer.entity.BuyerOrder;
import com.yhsoft.buyer.service.IBuyerOrderService;
import com.yhsoft.common.response.PageResult;
import com.yhsoft.common.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-09-26
 */
@Api(tags = {"G-wx-订单接口"})
@RestController
@RequestMapping("/buyer/Order")
public class BuyerOrderController {

    @Resource
    private IBuyerOrderService iBuyerOrderService;

    @Resource
    private ISysYszkService iSysYszkService;

    @Resource
    private ISysYfzkService iSysYfzkService;

    @ApiOperation("订单列表")
    @GetMapping("/list")
    public Result<PageResult<OrderInfoOutVo>> list(@RequestParam(value = "showType", required = false) @ApiParam(value = "0-已完成 1-待付款 2-代发货 3-待收货 4-已取消 5-支付中 6-支付失败 7-已退款") Integer showType,
                                                   @RequestParam(value = "pageNo") Long pageNo, @RequestParam(value =
            "pageSize") Long pageSize) {

        IPage<OrderInfoOutVo> convert = iBuyerOrderService.getList(showType, pageNo, pageSize);
        return Result.success(PageResult.valueOf(convert));
    }

    @ApiOperation("订单详情")
    @GetMapping("/detail")
    public Result<OrderDetailOutVo> detail(@RequestParam(value = "orderId") @ApiParam(value = "订单id") String orderId) {
        OrderDetailOutVo outVo = iBuyerOrderService.detail(orderId);
        return Result.success(outVo);
    }

    @ApiOperation("取消订单")
    @PostMapping("/cancel")
    public Result<?> cancel(@RequestParam(value = "orderId") String orderId) {
        BuyerOrder buyerOrder = iBuyerOrderService.getBaseMapper().selectById(orderId);
        if (buyerOrder == null) {
            return Result.fail("没查询到该订单，取消失败！");
        }
        buyerOrder.setOrderStatus(4);
        iBuyerOrderService.getBaseMapper().updateById(buyerOrder);
        // 商品货品数量增加
        iBuyerOrderService.increaseProductStock(buyerOrder);
        return Result.success("取消订单成功！");
    }

    @ApiOperation("订单退款")
    @PostMapping("/refund")
    private Result<?> refund(@RequestParam(value = "orderId") String orderId){
        BuyerOrder buyerOrder = iBuyerOrderService.getBaseMapper().selectById(orderId);
        if (buyerOrder == null) {
            return Result.fail("没查询到该订单，退款失败！");
        }
        // 只有未发货的或者支付失败的订单才能退款
        if (buyerOrder.getOrderStatus() != 5 && buyerOrder.getOrderStatus()!= 2) {
            return Result.fail("该订单无法退款！");
        }

        String result = iBuyerOrderService.refund(buyerOrder);
        return result.contains("SUCCESS") ? Result.success() : Result.fail(result);
    }

    @ApiOperation("确认收货/订单完成")
    @PostMapping("/confirm")
    public Result<?> confirm(@RequestParam(value = "orderId") String orderId) {
        BuyerOrder buyerOrder = iBuyerOrderService.getBaseMapper().selectById(orderId);
        if (buyerOrder == null) {
            return Result.fail("没查询到该订单，确认失败！");
        }
        buyerOrder.setOrderStatus(0);
        buyerOrder.setEndTime(LocalDateTime.now());
        iBuyerOrderService.getBaseMapper().updateById(buyerOrder);

        iSysYszkService.addYszk(buyerOrder.getTotalSalerPrice());
        iSysYfzkService.addYfzk(buyerOrder.getTotalOriginalPrice(),buyerOrder.getVendorId());

        return Result.success("确认成功！");
    }

    @ApiOperation("删除订单")
    @PostMapping("/delete")
    public Result<?> delete(@RequestParam(value = "orderId") String orderId) {
        BuyerOrder buyerOrder = iBuyerOrderService.getBaseMapper().selectById(orderId);
        if (buyerOrder == null) {
            return Result.fail("没查询到该订单，删除失败！");
        }
        iBuyerOrderService.getBaseMapper().deleteById(orderId);
        return Result.success("删除订单成功！");
    }

}
